using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using Telerik.WinControls.UI;
using BO;

namespace AnimalFarm.Product_manage
{
    public partial class ListMix : Telerik.WinControls.UI.RadForm
    {
        B_Building B_building = new B_Building();
        B_Mix B_mix = new B_Mix();
        int mixID;
        public ListMix()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        //load mix
        private void loadMix()
        {
            DataTable aTable = B_mix.GetAllMix();
            grvMix.DataSource = aTable;
        }

        //load building
        private void loadBuilding(int buildingID)
        {
            DataTable aTable = B_building.GetAllBuilding();
            drdBuilding.DataSource = aTable;
            drdBuilding.DisplayMember = "BuildingName";
            drdBuilding.ValueMember = "BuildingID";
            drdBuilding.SelectedValue = buildingID;
        }

        private void ListMix_Load(object sender, EventArgs e)
        {
            loadMix();
            loadContextMenu();
        }

        //load context menu
        private void loadContextMenu()
        {
            ContextMenu menu = new ContextMenu();
            MenuItem item = new MenuItem();
            item.Text = "Delete";
            menu.MenuItems.Add(item);
            grvMix.ContextMenu = menu;
            item.Click += new EventHandler(item_Click);
        }

        void item_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(this, "Are you sure", "Deleting mix", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                int mixID = int.Parse(grvMix.SelectedRows[0].Cells["MixID"].Value.ToString());
                bool isOk = B_mix.RemoveOneMix(mixID);
                if (isOk)
                {
                    MessageBox.Show(this, "Mix deleted successful", "Mix deleting", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadMix();
                }
                else
                {
                    MessageBox.Show(this, "Can not delete this Mix", "Mix deleting", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void grvMix_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvMix.SelectedRows.Count > 0)
                {
                    GridViewRowInfo row = grvMix.SelectedRows[0];
                    mixID = int.Parse(row.Cells["MixID"].Value.ToString());
                    string mixName=row.Cells["MixName"].Value.ToString();
                    DateTime time = DateTime.Parse(row.Cells["Time"].Value.ToString());
                    int buildingID = int.Parse(row.Cells["MixLocationID"].Value.ToString());
                    string description = row.Cells["Description"].Value.ToString();
                    int quanlity = int.Parse(row.Cells["Quanlity"].Value.ToString());

                    txtDescriptioin.Text = description;
                    txtMixName.Text = mixName;
                    txtQuanlity.Text = quanlity + "";
                    dtpTime.Value = time;
                    loadBuilding(buildingID);
                }
            }
            catch (Exception ex) { }
        }

        private void btnSaveChange_Click(object sender, EventArgs e)
        {
            try
            {
                Mix_Info info = new Mix_Info();
                info.Description = txtDescriptioin.Text;
                info.MixID = mixID;
                info.MixLocationID = int.Parse(drdBuilding.SelectedValue.ToString());
                info.MixName = txtMixName.Text;
                try
                {
                    info.Quanlity = int.Parse(txtQuanlity.Text);
                }
                catch (Exception ex) { info.Quanlity = 0; }
                info.Time = dtpTime.Value;

                bool isOk = B_mix.EditOneMix(info);
                if (isOk)
                {
                    MessageBox.Show(this, "Mix updated successful", "Mix updating", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadMix();
                }
                else
                {
                    MessageBox.Show(this, "Mix updating is fail", "Mix updating", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex) { }
        }
    }
}
